# 导包
from api.login import LoginAPI
from api.course import CourseAPI


class TestSelectCourseAPI:
    TOKEN = None

    # 前置处理
    def setup_method(self):
        # 初始化
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()
        # 获取验证码
        response_v = self.login_api.get_verify_code()
        # 登录
        login_data = {
            "username": "admin",
            "password": "HM_2023_test",
            "code": 2,
            "uuid": response_v.json().get("uuid")
        }
        response_l = self.login_api.login(test_data=login_data)
        # 提取登录成功的信息并保存
        TestSelectCourseAPI.TOKEN = response_l.json().get("token")
        print(TestSelectCourseAPI.TOKEN)

    def teardown(self):
        pass

    def test01_select_success(self):
        response = self.course_api.select_course(test_data="?name=测试开发提升课01",token=TestSelectCourseAPI.TOKEN)
        print(response.json())
        #断言
        assert 200 ==response.status_code
        assert "成功" in response.text
        assert 200 ==response.json().get("code")

    def test02_select_success(self):
        response = self.course_api.select_course(test_data="?subject=6",token="xxx")
        print(response.json())
        #断言
        assert 200 ==response.status_code
        assert "认证失败" in response.text
        assert 401 ==response.json().get("code")